HackerRank Gridland Metro
提出
6/30 Runtime Error :(
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'gridlandMetro' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER m
# 3. INTEGER k
# 4. 2D_INTEGER_ARRAY track
#
def gridlandMetro(n, m, k, track):
# Write your code here
city = [1 * m for _ in range(n)] for i in track:
city[i0-1][i1-1:i2] = 0 * len(city[i0-1][i1-1:i2]) ans = 0
for i in city:
ans += sum(i)
return ans
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) m = int(first_multiple_input1) k = int(first_multiple_input2) track = []
for _ in range(k):
track.append(list(map(int, input().rstrip().split())))
result = gridlandMetro(n, m, k, track)
fptr.write(str(result) + '\n')
fptr.close()
# 4 4 3
# 2 2 3
# 3 1 4
# 4 4 4
解答
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'gridlandMetro' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER m
# 3. INTEGER k
# 4. 2D_INTEGER_ARRAY track
#
# 1 1 2
# 1 3 4 # 3 > 2
# 1 1 3
# 1 2 4 # 4 > 3
def gridlandMetro(n, m, k, track):
# Write your code here
d = {}
total = n*m
for i in range(k):
# track is not present
if r not in d:
# track not overlapping then update total <- why total?
total -= c2 - c1 + 1
# track is overlapping then update
# main logic
tracks = 0
for r in d:
lamps = total - tracks
return lamps
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) m = int(first_multiple_input1) k = int(first_multiple_input2) track = []
for _ in range(k):
track.append(list(map(int, input().rstrip().split())))
result = gridlandMetro(n, m, k, track)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=Wu-zdz7NyhA
提出
26/31 test cases failed :(
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'gridlandMetro' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER n tate
# 2. INTEGER m yoko
# 3. INTEGER k
# 4. 2D_INTEGER_ARRAY track
#
def gridlandMetro(n, m, k, track):
# Write your code here
ans = 0
for r, s, e in track:
# print(cover)
nocover = -1
for s, e in cover:
if e == -1:
nocover += 1
continue
else:
ans += s-1
ans += m-e
ans += nocover * m
return ans
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) m = int(first_multiple_input1) k = int(first_multiple_input2) track = []
for _ in range(k):
track.append(list(map(int, input().rstrip().split())))
result = gridlandMetro(n, m, k, track)
fptr.write(str(result) + '\n')
fptr.close()